package com.prs.crm.dao.cqunicom.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cqunicom.BusinessRemindDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cqunicom.BusinessRemind;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.domain.hr.Person;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-28 下午01:51:08
 * 简单说明
 */

public class BusinessRemindDaoImpl extends BaseDaoImpl<BusinessRemind>
		implements BusinessRemindDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessRemind> getByPersonRead(final Person person) {
		 return (List<BusinessRemind>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(BusinessRemind.class);
				        	criteria.add(Restrictions.eq("readYN","1"));
	 			        	criteria.add(Restrictions.eq("companyRemind",person.getCompanys()));
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@Override
	public BusinessRemind getByBusinessRemind(final PoplarOrder order) {
		 return (BusinessRemind) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(BusinessRemind.class);
				        	criteria.add(Restrictions.eq("remindOrder",order));
				        	List<BusinessRemind>  k=criteria.list();
				            if(k.size()==0){
				            	return null;
				            }else{
							return k.get(0);
				            }      	
				        }
					}
			);
	}

	@Override
	public void saveOrupdate(BusinessRemind vv) {
		this.getHibernateTemplate().update(vv);
	}

	@Override
	public Integer delBusinessRemind() {
		return (Integer) this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
							Query query = session.createQuery("delete from BusinessRemind");
							return query.executeUpdate();
				        }   
				 } );
	}

}
